<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    <button class="layui-btn layuiadmin-btn-list" data-type="add">添加分类</button>
                </div>

                <table id="LAY-category-list" lay-filter="LAY-category-list"></table>

                <script type="text/html" id="table-category-operation">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加子分类</a>
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
            </div>
        </div>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['table', 'form', 'jquery', 'treetable'], function(){
        var $ = layui.$
        ,table = layui.table
        ,form = layui.form
        ,treetable = layui.treetable;

        // 渲染表格
        treetable.render({
            elem: '#LAY-category-list'
            ,url: '/app/ky_shouyin/admin/goods/category_list'
            ,treeColIndex: 1
            ,treeSpid: 0
            ,treeIdName: 'id'
            ,treePidName: 'parent_id'
            ,cols: [[
                {type: 'numbers', fixed: 'left'}
                ,{field: 'name', title: '分类名称', width: 260}
                ,{field: 'sort', title: '排序', width: 80, sort: true}
                ,{field: 'level', title: '层级', width: 80}
                ,{field: 'status', title: '状态', width: 100, templet: function(d){
                    return d.status == 1 ? '<span class="layui-badge layui-bg-green">启用</span>' : '<span class="layui-badge layui-bg-gray">禁用</span>';
                }}
                ,{title: '操作', width: 250, align: 'center', toolbar: '#table-category-operation'}
            ]]
            ,page: false
        });

        //监听工具条
        table.on('tool(LAY-category-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除该分类？', function(index){
                    $.ajax({
                        url: '/app/ky_shouyin/admin/goods/category_delete'
                        ,type: 'post'
                        ,data: {id: data.id}
                        ,success: function(res){
                            if(res.code == 0){
                                layer.msg(res.msg, {icon: 1});
                                treetable.reload('#LAY-category-list');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑分类'
                    ,content: '/app/ky_shouyin/admin/goods/category_edit?id=' + data.id
                    ,area: ['500px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find('#save');
                        
                        //监听提交
                        iframeWindow.layui.form.on('submit(save)', function(data){
                            var field = data.field;
                            field.id = obj.data.id;
                            
                            //提交修改
                            $.ajax({
                                url: '/app/ky_shouyin/admin/goods/category_edit'
                                ,type: 'post'
                                ,data: field
                                ,success: function(res){
                                    if(res.code == 0){
                                        layer.msg(res.msg, {icon: 1});
                                        treetable.reload('#LAY-category-list');
                                        layer.close(index);
                                    }else{
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            });
                        });
                        
                        submit.trigger('click');
                    }
                }); 
            } else if(obj.event === 'add'){
                layer.open({
                    type: 2
                    ,title: '添加子分类'
                    ,content: '/app/ky_shouyin/admin/goods/category_add?parent_id=' + data.id
                    ,area: ['500px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find('#save');
                        
                        //监听提交
                        iframeWindow.layui.form.on('submit(save)', function(data){
                            var field = data.field;
                            
                            //提交添加
                            $.ajax({
                                url: '/app/ky_shouyin/admin/goods/category_add'
                                ,type: 'post'
                                ,data: field
                                ,success: function(res){
                                    if(res.code == 0){
                                        layer.msg(res.msg, {icon: 1});
                                        treetable.reload('#LAY-category-list');
                                        layer.close(index);
                                    }else{
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            });
                        });
                        
                        submit.trigger('click');
                    }
                }); 
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加分类'
                    ,content: '/app/ky_shouyin/admin/goods/category_add'
                    ,area: ['500px', '400px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find('#save');
                        
                        //监听提交
                        iframeWindow.layui.form.on('submit(save)', function(data){
                            var field = data.field;
                            
                            //提交添加
                            $.ajax({
                                url: '/app/ky_shouyin/admin/goods/category_add'
                                ,type: 'post'
                                ,data: field
                                ,success: function(res){
                                    if(res.code == 0){
                                        layer.msg(res.msg, {icon: 1});
                                        treetable.reload('#LAY-category-list');
                                        layer.close(index);
                                    }else{
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            });
                        });
                        
                        submit.trigger('click');
                    }
                }); 
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
    </script>
</body>
</html> 